package com.kaishengit.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.pms.entity.Employee;
import com.kaishengit.pms.entity.Files;
import com.kaishengit.pms.util.DBHelp;
import com.kaishengit.pms.util.RowMapper;

public class FilesDao {
	

	private DBHelp<Files> db = new DBHelp<Files>();
	

		
		
		public class FilesRowMapper implements RowMapper<Files>{

			public Files mapperRow(ResultSet rs) throws SQLException {
				Files tf = new Files();
				tf.setId(rs.getString("id"));
				tf.setProjectid(rs.getString("projectid"));
				tf.setCreatetime(rs.getString("createtime"));
				tf.setEmployeeid(rs.getString("employeeid"));
				tf.setName(rs.getString("name"));
				
				Employee em = new Employee();
				em.setId(rs.getString("employeeid"));
				em.setPassword(rs.getString("password"));
				em.setUsername(rs.getString("username"));
	            em.setPic(rs.getNString("pic"));
				tf.setEmployee(em);
				
				tf.setEmployeeid(rs.getString("employeeid"));
				tf.setProjectid(rs.getString("projectid"));
				
				
				return tf;
			}
		}

		public List<Files> findAllFiles(String projectid) {
				StringBuilder sql = new StringBuilder();
				sql.append("SELECT * FROM t_filetype AS tf ");
				sql.append("LEFT JOIN t_employee AS te ON te.id = tf.employeeid ");
				sql.append("WHERE tf.projectid = ? ORDER BY createtime DESC");
				return db.executeQueryForList(sql.toString(), new FilesRowMapper(),projectid);
			}


	

	

	public void drop(String id) {
			String sql="DELETE FROM t_filetype WHERE id = ?";
			db.executeSQL(sql, id);
	}


	public void sava(String name, String employeeid, String projectid,
			String id, String now) {
		String sql = "insert into `t_filetype`(`id`,`name`,`createtime`,employeeid,projectid) values(?,?,?,?,?)";
		db.executeSQL(sql, id,name,now,employeeid,projectid);
		
	}
}
